package Modele;


import java.util.ArrayList;
import java.util.List;




public class CollectionFavoris implements ICollectionFavoris
{
    private ArrayList<IFavoris> listefav;
    private int size = 0;
    
    public CollectionFavoris() {
        listefav = new ArrayList<>();
    }

    public List<IFavoris> getListeFavoris(){
        return this.listefav;
    }
    
    public void addFavoris (IFavoris fav){
      
        boolean ecrire = true;
        for(int i = 0; i<this.getSize(); i++){
            if (fav.getNom().equals(listefav.get(i).getNom())){
                ecrire = false;
            }            
        }
        if (ecrire) {
            listefav.add(fav);
            size++;
        }
       
    }
    
    public void deleteFavoris (IFavoris fav){
        if (listefav.remove(fav)) size--;
    }
    
    public void deleteFavorisByNom(String nom){
              
        for(int i = 0; i<this.getSize(); i++){
            if (listefav.get(i).getNom().equals(nom)){
                listefav.remove(i);
                size--;
                break;
            }
            
        }
        
    }
    
    
    
    public void deleteFavorisById(int id){
        if ((id < this.getSize())&&(id >= 0)){
            IFavoris fav = listefav.get(id);
        
            if (fav != null) {
                   if (listefav.remove(fav)) size--;
            }
        }
        //if (listefav.remove(id) != null) size--;
    }
    
    public int getSize(){
        return size;
    }
    
    public IFavoris getFavoris(int index){
        if ((index < this.getSize())&&(index >= 0)) return listefav.get(index);
        return null;
    }
    
    public String toString(){
        String s = "";
       for(int i=0; i<this.getSize(); i++){
            s = s + listefav.get(i).getNom() + " "+ listefav.get(i).getURL() +"\n";
           
        }
         return s;
    }
    
    public void deleteAllFavoris(){
        listefav.clear();
        size= 0 ;
    }
}
